package com.rent.product.controller;


import com.rent.product.dao.PmsSpuDao;
import com.rent.product.entity.PmsAttrEntity;
import com.rent.product.entity.PmsSpuEntity;
import com.rent.product.entity.model.PmsSpumodel;
import com.rent.product.service.PmsSpuService;
import com.rent.product.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("product/pmsspu")
public class PmsSpuConstroller {

    /**
     *
     * @param waresIds
     * @return
     */

    @Autowired
    private PmsSpuService pmsSpuService;

    @Resource
    private PmsSpuDao pmsSpuDao;


    //属性分组所有数据显示
    @GetMapping("/spuc")
    public R spu(int cid, String key, HttpServletRequest request) throws IOException {


        int pageSize = Integer.parseInt(request.getParameter("limit").trim());//每页显示的记录数
        int nowPageIndex = Integer.parseInt(request.getParameter("page").trim());//当前显示的页码


        HashMap<String,Object> map=new HashMap<String, Object>();
        List<PmsSpuEntity> spuEntityList;
        PmsSpumodel pmsSpumodel=new PmsSpumodel();
        pmsSpumodel.setKey(key);
        int countspsu=0;
        //无参数时查询
        if(pmsSpumodel.getKey().isEmpty()){

            pmsSpumodel.setCatelogid(cid);
            pmsSpumodel.setPageSize(pageSize);
            int pageNumber=(nowPageIndex-1)*pageSize;
            pmsSpumodel.setPageNumber(pageNumber);
            //分页查询
            List<List<?>> test=pmsSpuDao.findtest(pmsSpumodel);
            spuEntityList=(List<PmsSpuEntity>) test.get(0);
            countspsu=(Integer)test.get(1).get(0);


        }
        //有参数时查询
        else {
            pmsSpumodel.setCatelogid(cid);
            int pageNumber=(nowPageIndex-1)*pageSize;
            pmsSpumodel.setPageNumber(pageNumber);
            pmsSpumodel.setPageSize(pageSize);
            //分页查询
            List<List<?>> test=pmsSpuDao.findother(pmsSpumodel);
            spuEntityList=(List<PmsSpuEntity>) test.get(0);
            countspsu=(Integer)test.get(1).get(0);
        }
        map.put("code",0);
        map.put("msg","");
        map.put("count",countspsu);
        map.put("data",spuEntityList);

        return  R.ok(map);
    }


    //规格参数||销售属性所有数据查询
    @GetMapping("/attr")
    public R spucattr(int cid, String key, HttpServletRequest request) throws IOException {

        int pageSize = Integer.parseInt(request.getParameter("limit").trim());//每页显示的记录数
        int nowPageIndex = Integer.parseInt(request.getParameter("page").trim());//当前显示的页码

        HashMap<String,Object> map=new HashMap<String, Object>();
        List<PmsSpuEntity> attrEntityList;
        PmsSpumodel pmsSpumodel=new PmsSpumodel();
        pmsSpumodel.setKey(key);
        int countspsu=0;
        //无参数时查询
        if(pmsSpumodel.getKey().isEmpty()){

            pmsSpumodel.setCatelogid(cid);
            pmsSpumodel.setPageSize(pageSize);
            int pageNumber=(nowPageIndex-1)*pageSize;
            pmsSpumodel.setPageNumber(pageNumber);
            //分页查询
            List<List<?>> test=pmsSpuDao.findattr(pmsSpumodel);
            attrEntityList=(List<PmsSpuEntity>) test.get(0);
            countspsu=(Integer)test.get(1).get(0);
        }
        //有参数时查询
        else {
            pmsSpumodel.setCatelogid(cid);
            int pageNumber=(nowPageIndex-1)*pageSize;
            pmsSpumodel.setPageNumber(pageNumber);
            pmsSpumodel.setPageSize(pageSize);
            //分页查询
            List<List<?>> test=pmsSpuDao.findtest1(pmsSpumodel);
            attrEntityList=(List<PmsSpuEntity>) test.get(0);
            countspsu=(Integer)test.get(1).get(0);
        }


        map.put("code",0);
        map.put("msg","");
        map.put("count",countspsu);
        map.put("data",attrEntityList);
        return  R.ok(map);
    }



    //属性分组新增
    @GetMapping("/add")
    public R add(String agname,int sort,String descript,String icon,int catelogid){

        PmsSpuEntity pmsSpuEntity=new PmsSpuEntity();

        pmsSpuEntity.setAgname(agname);
        pmsSpuEntity.setDescript(descript);
        pmsSpuEntity.setIcon(icon);
        pmsSpuEntity.setCatelogid(catelogid);
        pmsSpuEntity.setSort(sort);

        HashMap<String,Object> map=new HashMap<String, Object>();

        pmsSpuDao.attradd(pmsSpuEntity);

        map.put("msg","新增成功");

        return R.ok();
    }


    //属性参数规格新增

    @GetMapping("/add1")
    public  R add1(String valueselect){

        System.out.println(valueselect);
        PmsAttrEntity pmsAttrEntity=new PmsAttrEntity();
        pmsAttrEntity.setValueselect(valueselect);
        HashMap<String,Object> map=new HashMap<String, Object>();

        pmsSpuDao.add1(pmsAttrEntity);

        map.put("msg","新增成功");

        return R.ok(map);
    }


    //获取属性参数规格可选值
    @GetMapping("/getValueSelect")
    public  R getValueSelect(){


        return R.ok().put("data",pmsSpuService.getValueSelect());
    }










}
